package com.googlecode.monitoring.web;

import com.googlecode.monitoring.flash.dao.XmlFlashAgentStatusDao;
import com.googlecode.monitoring.model.ItemToCheck;
import com.googlecode.monitoring.ws.model.WsItemToCheck;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

public class FlashAgentStatusBean extends StatusBean {
    Logger log = Logger.getLogger(WsAgentStatusBean.class);
    private List<ItemToCheck> wsItems;
	private XmlFlashAgentStatusDao statusDao;

    private FlashAgentStatusBean() {
    }

    public FlashAgentStatusBean(XmlFlashAgentStatusDao statusDao, SettingsBean settings) throws IOException {
        super(settings);
		this.statusDao = statusDao;
    }

    @SuppressWarnings("unchecked")
    public List<WsItemToCheck> getData() throws IOException{
        log.debug("reading Flash data");
        updateData(statusDao.getXmlFlashStatusResource());
        if (isDataFound()) {
            wsItems = statusDao.getAllItemsFromCheckReport();
            setOutdatedItemsCount(calculateOutdatedItemsCount(wsItems));
        }
        if (wsItems != null){
            Collections.sort(wsItems);
        }
        return wsItems == null ? Collections.EMPTY_LIST : wsItems;
    }
}
